package com.ext.aic.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ext.aic.R;
import com.ext.aic.beans.Attachment;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.databinding.ElementItemViewBinding;
import com.ext.aic.enums.ElementLocationType;
import com.ext.aic.holder.ElementItemViewHolder;
import com.ext.aic.ui.CopywritingDetailActivity;
import com.ext.aic.utils.RedirectUtils;
import com.eyinfo.android_pure_utils.ObjectManager;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;
import com.eyinfo.android_pure_utils.utils.PixelUtils;

import java.util.ArrayList;
import java.util.List;

import lombok.Setter;

public class ElementAdapter extends RecyclerView.Adapter<ElementItemViewHolder> {

    @Setter
    private List<ProductItem> items = new ArrayList<>();
    private ElementLocationType locationType;
    private int imageSize;

    public ElementAdapter(Context context, ElementLocationType locationType) {
        this.locationType = locationType;
        int screenWidth = ObjectManager.getScreenWidth(context);
        int padding = PixelUtils.dip2px(context, 36);
        imageSize = (screenWidth - padding) / 2;
    }

    @Override
    public ElementItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.element_item_view, parent, false);
        return new ElementItemViewHolder(ElementItemViewBinding.bind(view));
    }

    @Override
    public void onBindViewHolder(ElementItemViewHolder holder, int position) {
        ProductItem productItem = items.get(position);
        holder.itemRl.setTag(position);
        holder.itemRl.setOnClickListener(this::onItemClick);
        ViewGroup.LayoutParams layoutParams = holder.imgIv.getLayoutParams();
        layoutParams.width = imageSize;
        layoutParams.height = imageSize;
        holder.imgIv.setLayoutParams(layoutParams);
        holder.titleTv.setText(productItem.getTitle());
        List<Attachment> attachments = productItem.getAttachments();
        if (!ObjectJudge.isNullOrEmpty(attachments)) {
            Attachment attachment = attachments.get(0);
            Glide.with(holder.imgIv)
                    .load(attachment.getFileUrl())
                    .into(holder.imgIv);
        }
        if (locationType == ElementLocationType.mine) {
            holder.elementBottomLl.setVisibility(View.GONE);
        }
    }

    public void onItemClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        ProductItem productItem = items.get(position);
        Bundle bundle = new Bundle();
        bundle.putString("productId", productItem.getId());
        bundle.putString("creatorId", productItem.getCreatedBy());
        RedirectUtils.getInstance().startActivity(view.getContext(), CopywritingDetailActivity.class, bundle);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
